# coding:utf-8
from .. import db 
from . import main
from flask import  render_template,request,url_for,session,redirect
from ..model import Teachers,Kinds,Couses,Videos
import random,datetime
# 如果加载的有js文件，那么项目文件的路径不能有中文，否则加载不出来
# 装饰器不是app.route,变成main.route
@main.route('/<name>')
def index(name):
    kinds = db.session.query(Kinds).all()
    # 先查询当前分类的内容
    kind = None
    if name=='course': 
        kind = db.session.query(Kinds).first()
    else:
        kind = db.session.query(Kinds).filter(Kinds.name==name).first()
    courses = kind.couses
    # 封装数据源
    data = {'islogin':session.get('islogin'),'courses':courses,'kinds':kinds,'current_kind':kind.name}
    return render_template('index.html',data=data)
# @main.route('/index/<name>')
# def current_index(name):
#     kind = db.session.query(Kinds).filter(Kinds.name==name).first()
#     kinds = db.session.query(Kinds).all()
#     courses = kind.couses
#     data = {'name':session['name'],'courses':courses,'kinds':kinds,'current_kind':kind.name}
#     if 'name' in session:
#         return render_template('index.html',data=data)

@main.route('/add')
def add():
    for item in range(1,8):
        name = 't_%s'%item
        abstract = 't_abstract'*random.randint(1,10)
        num = random.randint(0,1)
        level=''
        if num==0:
            level='助教'
        else:
            level='讲师'
        teachers = Teachers(name=name,abstract=abstract,level=level)
        db.session.add(teachers)
        db.session.commit()
    for item in range(1,8):
        name = 'name_%s' %item
        kind = Kinds(name=name)
        db.session.add(kind)
        db.session.commit()

    for item in range(1,20):
        title = 'title_%s' %item 
        abstract = '_abstract' * random.randint(1,6)
        addtime = datetime.datetime.now()
        # 外键范围，从数据库查看，然后在设置
        k_id = random.randint(1,7)
        course = Couses(title=title,abstract=abstract,addtime=addtime,k_id=k_id)
        db.session.add(course)
        db.session.commit()

    for item in range(1,60):
        title = 'title_%s' %item 
        time = random.randint(400,600)
        hits = random.randint(1000,2000)
        addtime = datetime.datetime.now()
        abstract = 'abstract'*random.randint(6,10)
        t_id  = random.randint(1,8)
        c_id = random.randint(1,19)
        video = Videos(title=title,time=time,hits=hits,addtime=addtime,abstract=abstract,t_id=t_id,c_id=c_id)
        db.session.add(video)
        db.session.commit()
    return 'add success'

@main.route('/commit_delete_course',methods=['POST','GET'])
def commit_delete_course():
    id=request.form['id']
    print '>>>>>>>>>',id
    course = db.session.query(Couses).filter(Couses.id ==id).first()
    db.session.delete(course)
    db.session.commit()
    return 'delete success'

@main.route('/course_edit')
def course_edit():
    id = request.args.get('id',None)
    
    data = db.session.query(Couses).filter(Couses.id==id).first()
    return render_template('course_edit.html',data=data)
@main.route('/commit_edit_course',methods=['POST'])
def commit_edit_course():
    # id = request.args.get('id',None)
    id = request.form['id']
    data = db.session.query(Couses).filter(Couses.id==id).first()
    # kind = data.kinds
    data.title = request.form['title']
    data.abstract = request.form['abstract']
    db.session.commit()
    # return redirect('/'+kind.name)
    
    return 'edit success'
@main.route('/video_show')
def video_show():
    c_id = request.args.get('id',None)
    videos = db.session.query(Videos).filter(Videos.c_id==c_id).all()
    l = []
    for item in videos:
        teacher =db.session.query(Teachers).filter(Teachers.id==item.t_id)
        l.append(teacher.name)
    data={'videos':videos,'c_id':c_id,'teachers':l}
    return render_template('video_show.html',data=data)
@main.route('/video_edit')
def video_edit():
    id = request.args.get('id',None)
    video = db.session.query(Videos).filter(Videos.id==id).first()
    return render_template('video_edit.html',video=video)
@main.route('/commit_edit_video',methods=['POST'])
def commit_edit_video():
     id = request.args['id']
     video = db.session.query(Videos).filter(Videos.id==id).first()
     course = video.courses
     video.title = request.form['title']
     video.t_id = request.form['teacher']
     video.hits = request.form['hits']
     video.time = request.form['time']
     video.abstract = request.form['abstract']
     db.session.commit()
     return redirect(url_for('main.video_show',id=course.id))
@main.route('/commit_delete_video',methods=['GET','POST'])
def commit_delete_video():
    id = request.args['id']
    print '>>>>>>>>>>',id
    video = db.session.query(Videos).filter(Videos.id==id).first()
    course = video.courses
    db.session.delete(video)
    db.session.commit()
    return redirect(url_for('main.video_show',id=course.id))
@main.route('/add_video')
def add_video():
    id = request.args['id']
    return render_template('add_video.html',id=id)
@main.route('/commit_add_video',methods=['POST'])
def commit_add_video():
     id = request.args['id']
     
     title = request.form['title']
     t_id = request.form['teacher']
     hits = request.form['hits']
     time = request.form['time']
     abstract = request.form['abstract']
     video = Videos(title=title,c_id=id,t_id=t_id,hits=hits,time=time,abstract=abstract)
     db.session.add(video)
     db.session.commit()
     return redirect(url_for('main.video_show',id=id))

@main.route('/add_course/<k_name>',methods = ['POST','GET'])
def add_course(k_name):
    if request.method == 'POST':
        name = request.form.get('name')
        abstract = request.form.get('abstract')

        kind = db.session.query(Kinds).filter_by(name = k_name).first()
        k_id = kind.id

        course = Couses(title = name,addtime=datetime.datetime.now(),k_id = k_id,abstract=abstract)
        db.session.add(course)
        db.session.commit()
        return redirect(url_for('main.index',name=k_name))
    return render_template('add_course.html')


